package com.guoxin.designpattern.chapter5;

/**
 * sharedResource
 *
 * @author guoxin
 * @date 2019年9月4日
 */
public class Gate {
    private int counter = 0;
    private String name = "Nobody";
    private String adderss = "Nowhere";

    /**
     * 临界值
     * @param name
     * @param address
     */
    public synchronized void pass(String name, String address) {
        counter++;
        this.name = name;
        this.adderss = address;
        verify();
    }

    private void verify() {
        if (name.charAt(0) != adderss.charAt(0)) {
            System.out.println("*****BROKEN*****" + toString());
        }
    }

    @Override
    public synchronized String toString() {
        return "No." + counter + ":" + name + "," + adderss;
    }

}
